<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath();
%>
<jsp:include page="/WEB-INF/page/auth/_header.jsp"></jsp:include>
<body>
	<jsp:include page="/WEB-INF/page/auth/header.jsp"></jsp:include>
	<div id="middle">
		<jsp:include page="/WEB-INF/page/auth/menu.jsp"></jsp:include>
		<div id="right">
		    <jsp:include page="/WEB-INF/page/auth/localinfo.jsp" />
			<div class="col-md-12" id="content_div">
				<div class="easyui-layout" style="width:100%;height:95%;">
	                <div data-options="region:'west',title:'菜单类别',iconCls:'icon-ok'" 
	                  style="width:20%;">
	                  <a href="javascript:void(0)" class="easyui-linkbutton"  plain="true" onclick="updateDictType()"><i class="fa fa-edit"></i> 编辑</a>
	                  <ul id="dataTypeAllMenu"></ul>
	                </div>
	                <div data-options="region:'center'">
	                    <table id="dataGrid" class="easyui-datagrid"></table>
	                    
	                    <div id="toolbar">
	                        <a href="javascript:void(0)" class="easyui-linkbutton"  plain="true" onclick="addDictData()"><i class="fa fa-plus"></i> 新增</a>
	                    </div>
	                </div>
	            </div>
			</div>
		</div>
	</div>
	<div id="addDictType" class="easyui-dialog" style="width: 400px;height:auto;"  closed="true" >
	    <form id="addDictTypeForm" class="easyui-form form-search" >
	    	<input type="hidden" id="id" name="id" />
	        <table class="table" style="margin:0 auto;">
	        	<tr>
	                <td width="30%" class="text-right" >菜单类别</td>
	                <td width="60%">
	                    <input name="dictDataValue" id="dictDataValue" class="form-control">
	                </td>
	            </tr>
	        </table>
	    </form>
	
	    <div id="addDictTypeButton" class="text-center pd10">
	        <a href="javascript:void(0)" class="btn btn-success" onclick="submitDictType()"  plain="true">保存</a>
	    </div>
	</div>
	
	
	<div id="addDictData" class="easyui-dialog" style="width: 400px;height:auto;"  closed="true" >
	    <form id="addDictDataForm" class="easyui-form form-search" >
	    	<input type="hidden" id="id" name="id" />
	    	<input type="hidden" id="dictDataKey" name="dictDataKey" />
	        <input type="hidden" id="dictTypeId" name="dictTypeId" />
	        <table class="table" style="margin:0 auto;">
	        	<tr>
	                <td width="30%" class="text-right" >名称</td>
	                <td width="60%">
	                    <input name="dictDataValue2" id="dictDataValue2" class="form-control">
	                </td>
	            </tr>
	            <tr>
	                <td width="30%" class="text-right" >描述</td>
	                <td width="60%">
	                	<textarea class="form-control" name="desc" id="desc" rows="4"></textarea>
	                </td>
	            </tr>
	            <tr>
	                <td class="text-right" >是否启用</td>
	                <td>
	                    <select name="isActive" id="isActive" class="form-control">
	                        <option value="1">启用</option>
	                        <option value="0">禁用</option>
	                    </select>
	                </td>
	            </tr>
	        </table>
	    </form>
	
	    <div id="addDictDataButton" class="text-center pd10">
	        <a href="javascript:void(0)" class="btn btn-success" onclick="submitDictData()"  plain="true">保存</a>
	    </div>
	</div>
	<jsp:include page="/WEB-INF/page/auth/_footer.jsp"/>

	<script type="text/javascript">
		var url = basePath + "/sys/wechatMenu/conf";
		var addUpdateDictTypeUrl = url + "/updateType";
		var addUpdateDictDataUrl = url + "/addUpdate";
	
		var dataDataGrid;
		var confirmDialog;
		$(function(){
		    initDictType();
		});
	
		/**
		 * 初始化字典类别
		 */
		function initDictType(){
		    $.ajax({
		        url: basePath + "/dictData/dictCode",
		        type: 'POST',
		        dataType: 'json',
		        data:{
		        	"dictCode": "wechat_menu"
		        },
		        error : function(data) {
		            $.messager.progress('close');
		            $.messager.alert('系统提示', data.msg, 'error');
		        },
		        success: function (data) {
		        	var currId = $("#dataTypeAllMenu").find("li.dict-cur").find('a').attr("id");
		            var html = '';
		            data.forEach(function(item,index){
		            	var clasz="";
		            	if(!currId && index == 0){
		            		clasz="class=\"dict-cur\"";
		            		currId = item.id;
		            	}else {
		            		if(item.id == currId) clasz="class=\"dict-cur\"";
		            	}
		                html += '<li ' + clasz + '><a href="javascript:void(0)" id="' + item.id + '" onclick="linkDictDate(this)" class="tdictType_link">'+item.dictDataValue+'</a></li>'
		            });
		            initDictData(currId);
		            $("#dataTypeAllMenu").html(html);
		        }
		    });
	
		}
	
		/**
		 * 菜单数据表
		 */
		function initDictData(id){
		    dataDataGrid = $('#dataGrid').datagrid({
		        url : url + "/pageQuery",
		        fit : true,
		        fitColumns : true,
		        border : true,
		        singleSelect : true,
		        pagination : false,
		        nowrap : true,
		        striped : true,
		        toolbar:'#toolbar',
		        queryParams : {
		            "menuType": id
		        },
		        columns : [ [{
			        	field : 'action',
			        	title : '操作',
			        	align : 'center',
			        	formatter : function(value, row, index) {
			        		return $.formatString('<button  type="button" class="btn btn-warning btn-xs" style="margin:4px 4px;" onclick="updateDictData(\'{0}\');">编辑</button>', row.id);
			        	}
					}, {
						field : 'isActive',
						align : 'center',
						title : '<b>状态</b>',
		            	formatter:function(value,row,index){
		                    if(value == 1){
		                        return "启用";
		                    }else{
		                        return "禁用";
		                    }
		                }
		            }, {
		            	field : 'dictDataKey',
		            	align : 'center',
		            	title : '<b>字典数据编码</b>'
		            }, {
		            	field : 'dictDataValue',
		            	align : 'center',
		            	title : '<b>字典数据名称</b>'
		            }, {
		            	field : 'desc',
		            	align : 'center',
		            	title : '<b>描述</b>',
		            	width : 10,
		            	formatter : function(value, row, index) {
		            		value = value ? value : "";
		            		return "<a title='" + value + "'>"+ value +"</a>";
		            	}
		            }, {
		            	field : 'creater',
		            	align : 'center',
		            	title : '<b>创建人</b>'
		            }, {
		            	field : 'createTime',
		            	align : 'center',
		            	title : '<b>创建时间</b>', 
		            	formatter : function(value) {
		    				return getTime(value, "yyyy-MM-dd hh:mm");
		    			}
		            }
		        ]]
		    });
		}
	
		/**
		 * 清空表单
		 */
		var cleanFormPanel=function(formId){
			$('#' + formId)[0].reset();
			$('#' + formId + " #id").val(null);
		}
	
		function addDictType(){
			cleanFormPanel("addDictTypeForm");
		    $('#addDictType').dialog('open').dialog('center').dialog('setTitle','修改菜单类别');
		}
	
		function addDictData(){
			cleanFormPanel("addDictDataForm");
			$("#addDictDataForm #dictTypeId").val($(".dict-cur >a")[0].id);
		    $('#addDictData').dialog('open').dialog('center').dialog('setTitle','新增字典类别');
		}
	
		function updateDictType() {
			var id = $(".dict-cur >a")[0].id;
			$.ajax({
		 		url : url + "/queryType/" + id,
		 		type : "GET",
		 		dataType : "json",
		 		async : false,
		 		success : function(data) {
					addDictType();
					$("#addDictTypeForm #id").val(data.id);
					$("#addDictTypeForm #dictDataValue").val(data.dictDataValue);
		 		}
		 	});
		}
	
		function updateDictData(id) {
			$.ajax({
		 		url : dictDataByIdUrl,
		 		type : "post",
		 		data : {
		 			id : id
		 		},
		 		dataType : "json",
		 		async : false,
		 		success : function(data) {
					addDictData();
					$("#addDictDataForm #id").val(data.id);
					$("#addDictDataForm #dictTypeId").val(data.dictTypeId);
					$("#addDictDataForm #dictDataKey").val(data.dictDataKey);
					$("#addDictDataForm #dictDataValue").val(data.dictDataValue);
					$("#addDictDataForm #desc").val(data.desc);
					$("#addDictDataForm #isActive").val(data.isActive);
		 		}
		 	});
		}
	
		function submitDictType() {
			var dictDataValue = $("#dictDataValue").val();
			if(dictDataValue.length == 0) {
				$.messager.alert('系统提示', '类别编码不能为空', 'error');
				return;
			}
			commitDictType();
		}
	
		function commitDictType(){
			$.ajax({
				url : url + "/updateType",
				type : 'POST',
				error : function() {
					$.messager.progress('close');
					$.messager.alert('系统提示', '操作异常', 'error');
				},
				data : $('#addDictTypeForm').serialize(),
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						$.messager.alert('系统提示', $("#addDictTypeForm #id").val() == '' ? '菜单类别新增成功' : '菜单类别修改成功', 'info');
						initDictType();
						cleanFormPanel("addDictTypeForm");
						$('#addDictType').dialog('close')
					} else {
						$.messager.alert('系统提示', $("#addDictTypeForm #id").val() == '' ? '菜单类别新增失败' : '菜单类别修改失败', 'warning');
					}
				}
			});
		}
	
		function submitDictData() {
			var dictDataValue = $("#dictDataValue").val();
			if(dictDataValue.length == 0) {
				$.messager.alert('系统提示', '名称不能为空', 'error');
				$("#dictDataValue").focus();
				return;
			}
			$.ajax({
				url : addUpdateDictDataUrl,
				type : 'POST',
				error : function() {
					$.messager.progress('close');
					$.messager.alert('系统提示', '操作异常', 'error');
				},
				data : $('#addDictDataForm').serialize(),
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						$.messager.alert('系统提示', $("#addDictDataForm #id").val() == '' ? '字典类别新增成功' : '字典类别修改成功', 'info');
						initDictType();
						cleanFormPanel("addDictDataForm");
						$('#addDictData').dialog('close')
					} else {
						$.messager.alert('系统提示', $("#addDictDataForm #id").val() == '' ? '字典类别新增失败' : '字典类别修改失败', 'warning');
					}
				}
			});
		}
	
		function linkDictDate(thiz) {
			var lic = $(thiz.closest('li'));
			if(!lic.hasClass('dict-cur')) {
				initDictData(thiz.id);
				lic.addClass("dict-cur").siblings().removeClass("dict-cur");
			}
		}
	
		$("#dictCode").on("blur", function() {
			var value = this.value;
			if(value.length == 0) return;
			if (!(/^[A-Za-z_-]+$/).test(value)) {
				$.messager.alert('系统提示', '只能输入字母', 'warning');
			}
			$.ajax({
		 		url : dictCodeExistsUrl,
		 		type : "post",
		 		data : {
		 			dictCode : this.value
		 		},
		 		dataType : "json",
		 		async : false,
		 		success : function(data) {
		 			if(data>0)
		 			 $.messager.alert('系统提示', '输入的编码已存在，请重新输入', 'warning');
		 		}
		 	});
		});
    </script>
</body>
</html>